package cn.doctorpda.conference.ui.personal.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.presenter.GetCodePresenter;
import cn.doctorpda.conference.presenter.RegisterPresenter;
import cn.doctorpda.conference.utils.MyTextUtil;
import cn.doctorpda.conference.utils.T;
import cn.doctorpda.conference.utils.TimeCountUtil;
import cn.doctorpda.conference.view.callbackview.IBaseView;
import cn.doctorpda.conference.view.callbackview.IMsgView;
import cn.doctorpda.conference.view.callbackview.IRegisterView;
import okhttp3.Call;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 */
public class RegisterDialogFragment extends DialogFragment implements
        OnClickListener, IRegisterView, IMsgView{

    private FragmentActivity mActivity;
    private EditText et_number, et_get_code, et_psw, et_nick_name;
    private String strNumber, strVerifyCode, strName, strPassWord;

    private RegisterPresenter registerPresenter;
    private GetCodePresenter mGetCodePresenter;
    private TextView tv_get_code;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mActivity = getActivity();
        View inflate = inflater.inflate(R.layout.fragment_register, null);
        inflate.findViewById(R.id.btn_register).setOnClickListener(this);
        inflate.findViewById(R.id.btn_close).setOnClickListener(this);
        inflate.findViewById(R.id.btn_existing_account).setOnClickListener(this);
        tv_get_code = (TextView) inflate.findViewById(R.id.btn_get_code);
        tv_get_code.setOnClickListener(this);
        initView(inflate);


        getDialog().getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        registerPresenter = new RegisterPresenter(this);
        mGetCodePresenter = new GetCodePresenter(this);
//        getDialog().setCanceledOnTouchOutside(false);
        return inflate;
    }

    private void initView(View inflate) {
        et_number = (EditText) inflate.findViewById(R.id.et_phone_number);
        et_get_code = (EditText) inflate.findViewById(R.id.et_identifying_code);
        et_nick_name = (EditText) inflate.findViewById(R.id.et_nick_name);
        et_psw = (EditText) inflate.findViewById(R.id.et_new_psw);

    }


    @Override
    public void onClick(View v) {

        strNumber = et_number.getText().toString().trim();
        strVerifyCode = et_get_code.getText().toString().trim();
        strName = et_nick_name.getText().toString().trim();
        strPassWord = et_psw.getText().toString().trim();

//        dismiss();
        switch (v.getId()) {
            case R.id.btn_register:

                if (strNumber.isEmpty()) {
                    T.showShort(getContext(), "手机号不能为空");
                    break;
                }
                if (!MyTextUtil.isPhoneNumber(strNumber)) {
                    T.showShort(getContext(), "手机号不正确");
                    break;
                }
                if (strVerifyCode.isEmpty()) {
                    T.showShort(getContext(), "验证码不能为空");
                    break;
                }
                if (!MyTextUtil.checkPassword(strPassWord)) {
                    T.showShort(getContext(), "密码格式不对");
                    break;
                }
                if (strPassWord.isEmpty()) {
                    T.showShort(getContext(), "密码不能为空");
                    break;
                }
                if (strName.isEmpty()) {
                    T.showShort(getContext(), "昵称不能为空");
                    break;
                }
                registerPresenter.getRegisterData(strNumber, strVerifyCode, strName, strPassWord);
                break;

            case R.id.btn_get_code:
                if (strNumber.isEmpty()) {
                    T.showShort(getContext(), "手机号不能为空");
                    break;
                }
                if (!MyTextUtil.isPhoneNumber(strNumber)) {
                    T.showShort(getContext(), "手机号不正确");
                    break;
                }
                TimeCountUtil timeCountUtil = new TimeCountUtil(60000, 1000,
                        tv_get_code);
                timeCountUtil.start();
                mGetCodePresenter.getCode(strNumber);
                break;
            case R.id.btn_existing_account:
                this.dismiss();
                break;

            case R.id.btn_close:
                this.dismiss();
                break;

            default:
                break;
        }
    }


    @Override
    public void setRegisterView(Msg msg) {
        if (msg.isSuccess()) {
            this.dismiss();
//            T.showShort(getContext(),"欢迎到医会通");
        }else {
            T.showShort(getContext(),"注册失败，请稍后再试");
        }
    }

    @Override
    public void setRegisterError(Call call, Exception e) {

    }


    @Override
    public void setResult(Msg msg) {
        T.showShort(getActivity(), msg.getMsg());
    }

    @Override
    public void setError(Call call, Exception e) {

    }
}
